home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 4 / Macwelt DVD 4.cdr / Entwickler / Mac-OS X / Pantomime / Source / LocalMessage.m / LocalMessage.m
Encoding:
Text File  |  2002-08-20  |  4.1 KB  |  173 lines

  1. /*
  2. **  LocalMessage.m
  3. **
  4. **  Copyright (c) 2001, 2002
  5. **
  6. **  Author: Ludovic Marcotte <ludovic@Sophos.ca>
  7. **
  8. **  This library is free software; you can redistribute it and/or
  9. **  modify it under the terms of the GNU Lesser General Public
  10. **  License as published by the Free Software Foundation; either
  11. **  version 2.1 of the License, or (at your option) any later version.
  12. **  
  13. **  This library is distributed in the hope that it will be useful,
  14. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. **  Lesser General Public License for more details.
  17. **  
  18. **  You should have received a copy of the GNU Lesser General Public
  19. **  License along with this library; if not, write to the Free Software
  20. **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22.  
  23. #import <Pantomime/LocalMessage.h>
  24.  
  25. #import <Pantomime/Constants.h>
  26. #import <Pantomime/LocalFolder.h>
  27. #import <Pantomime/NSDataExtensions.h>
  28.  
  29. #import <string.h>
  30.  
  31. @implementation LocalMessage 
  32.  
  33. //
  34. // NSCoding protocol
  35. //
  36. - (void) encodeWithCoder: (NSCoder *) theCoder
  37. {
  38.   //NSDebugLog(@"Encoding LocalMessage...");
  39.   [super encodeWithCoder: theCoder];
  40.   
  41.   [theCoder encodeObject: [NSNumber numberWithLong: [self filePosition]]];
  42.   [theCoder encodeObject: [NSNumber numberWithLong: [self bodyFilePosition]]];
  43. }
  44.  
  45. - (id) initWithCoder: (NSCoder *) theCoder
  46.   self = [super initWithCoder: theCoder];
  47.   
  48.   //NSDebugLog(@"Decoding LocalMessage...");
  49.  
  50.   [self setFilePosition: [[theCoder decodeObject] longValue]];
  51.   [self setBodyFilePosition: [[theCoder decodeObject] longValue]];
  52.   
  53.   return self;
  54. }
  55.  
  56. //
  57. // access / mutation methods
  58. //
  59. - (long) filePosition
  60. {
  61.   return filePosition;
  62. }
  63.  
  64. - (void) setFilePosition: (long) theFilePosition
  65. {
  66.   filePosition = theFilePosition;
  67. }
  68.  
  69. - (long) bodyFilePosition
  70. {
  71.   return bodyFilePosition;
  72. }
  73.  
  74. - (void) setBodyFilePosition: (long) theBodyFilePosition
  75. {
  76.   bodyFilePosition = theBodyFilePosition;
  77. }
  78.  
  79. - (NSData *) rawSource
  80. {
  81.   NSMutableData *aMutableData;
  82.   LocalFolder *aLocalFolder;
  83.   char aLine[1024];
  84.  
  85.   FILE *aStream;
  86.   long mark;
  87.  
  88.   aLocalFolder = (LocalFolder *)[self folder];
  89.   aStream = [aLocalFolder stream];
  90.  
  91.   mark = ftell(aStream);
  92.  
  93.   if (fseek(aStream, [self filePosition], SEEK_SET) < 0)
  94.     {
  95.       NSDebugLog( @"Seek operation failed!" );
  96.       return nil;
  97.     }
  98.  
  99.   // We initialize our mutable data and our buffer
  100.   aMutableData = [[NSMutableData alloc] init];
  101.   memset(aLine, 0, 1024);
  102.  
  103.   while( fgets(aLine, 1024, aStream) != NULL && 
  104.      ftell(aStream) < ([self filePosition] + [self size]) )
  105.     {
  106.       [aMutableData appendBytes: aLine  length: strlen(aLine) ];
  107.       memset(aLine, 0, 1024);
  108.     }
  109.  
  110.   fseek(aStream, mark, SEEK_SET);
  111.  
  112.   return AUTORELEASE(aMutableData);
  113. }
  114.  
  115. //
  116. // This method is called to initialize the message if it wasn't.
  117. // If we set it to NO and we HAD a content, we release the content;
  118. //
  119. - (void) setInitialized: (BOOL) aBOOL
  120. {
  121.   [super setInitialized: aBOOL];
  122.  
  123.   if ( aBOOL )
  124.     {
  125.       NSMutableData *aMutableData;
  126.       LocalFolder *aLocalFolder;
  127.       FILE *aStream;
  128.       char aLine[1024];
  129.       
  130.       int lengthOfBody;
  131.       long mark;
  132.       
  133.       
  134.       aLocalFolder = (LocalFolder *)[self folder];
  135.       aStream = [aLocalFolder stream];
  136.       
  137.       mark = ftell(aStream);
  138.       
  139.       if (fseek(aStream, [self bodyFilePosition], SEEK_SET) < 0)
  140.     {
  141.       NSDebugLog( @"Seek operation failed!" );
  142.       [super setInitialized: NO];
  143.       return;
  144.     }
  145.      
  146.       // We calculate the length of our body to make parsing faster
  147.       lengthOfBody = ([self filePosition] + [self size] - [self bodyFilePosition]);
  148.       aMutableData = [[NSMutableData alloc] initWithCapacity: lengthOfBody];
  149.       
  150.       memset(aLine, 0, 1024);
  151.       while( fgets(aLine, 1024, aStream) != NULL && 
  152.          ftell(aStream) < ([self filePosition] + [self size]) )
  153.     {
  154.       [aMutableData appendBytes: aLine  length: strlen(aLine) ];
  155.       memset(aLine, 0, 1024);
  156.     }
  157.       
  158.       
  159.       fseek(aStream, mark, SEEK_SET);
  160.       
  161.       [self setContentFromRawSource: aMutableData];
  162.       RELEASE(aMutableData);
  163.     }
  164.   else
  165.     {
  166.       TEST_RELEASE(content);
  167.       content = nil;
  168.     }
  169. }
  170.  
  171. @end
  172.